    var postsData = require('../../../data/posts-data.js');
var app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPlayingMusic: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        var postId = options.id
        this.setData({
            currentPostId: postId
        })
        var postData = postsData.postList[postId]
        // 如果在onLoad方法中，不是异步的去执行一个数据绑定
        // 则不需要使用this.setData
        // 只需要对this.data赋值即可实现数据绑定
        this.setData({
            postData: postData
        })

        var postsCollected = wx.getStorageSync('posts_collected')
        if (postsCollected) {
            var postCollected = postsCollected[postId]
            this.setData({
                collected: postCollected
            })
        } else {
            var postsCollected = {};
            postsCollected[postId] = false;
            wx.setStorageSync('posts_collected', postsCollected);
        }

        if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === this.data.currentPostId) {
            this.setData({
                isPlayingMusic: true
            })
        }

        // 调用函数
        this.setMusicMonitor()
    },

    // 监听音乐播放、暂停
    setMusicMonitor () {
        var that = this
        wx.onBackgroundAudioPlay(function () {
            that.setData({
                isPlayingMusic: true
            })
            app.globalData.g_isPlayingMusic = true
            app.globalData.g_currentMusicPostId = that.data.currentPostId
        })
        wx.onBackgroundAudioStop(function () {
            that.setData({
                isPlayingMusic: false
            })
            app.globalData.g_isPlayingMusic = false
            app.globalData.g_currentMusicPostId = null
        })
    },

    // 点击收藏
    onColletionTap () {
        this.getPostsCollectedAsy()
        // this.getPostsCollectedSyc()
    },

    // 异步
    getPostsCollectedAsy () {
        var that = this
        wx.getStorage({
            key: 'posts_collected',
            success: function (res) {
                var postsCollected = wx.getStorageSync('posts_collected')
                var postCollected = postsCollected[that.data.currentPostId]
                // 收藏变成未收藏，未收藏变成收藏
                postCollected = !postCollected
                postsCollected[that.data.currentPostId] = postCollected

                that.showToast(postsCollected, postCollected)
            }
        })
    },

    // 同步
    getPostsCollectedSyc () {
        var postsCollected = wx.getStorageSync('posts_collected')
        var postCollected = postsCollected[this.data.currentPostId]
        // 收藏变成未收藏，未收藏变成收藏
        postCollected = !postCollected
        postsCollected[this.data.currentPostId] = postCollected

        this.showToast(postsCollected, postCollected)
    },

    // 显示模态弹窗
    showModal (postsCollected, postCollected) {
        var that = this
        wx.showModal({
            title: '收藏',
            content: postCollected ? '收藏该文章？' : '取消收藏该文章？',
            showCancel: 'true',
            cancelText: '取消',
            cancelColor: '#333',
            confirmText: '确认',
            confirmColor: '#405f80',
            success: function (res) {
                if (res.confirm) {
                    // 更新文章是否的缓存值
                    wx.setStorageSync('posts_collected', postsCollected)
                    // 更新数据绑定变量，从而实现切换图片
                    that.setData({
                        collected: postCollected
                    })
                }
            }
        })
    },

    // 弹窗
    showToast: function (postsCollected, postCollected) {
        // 更新文章是否的缓存值
        wx.setStorageSync('posts_collected', postsCollected)
        // 更新数据绑定变量，从而实现切换图片
        this.setData({
            collected: postCollected
        })

        wx.showToast({
            title: postCollected ? '收藏成功' : '取消成功',
            icon: 'success',
            duration: 1000,
            mask: true
        })
    },

    // 分享
    onShareTap: function () {
        var itemList = [
            '分享给微信好友',
            '分享到朋友圈',
            '分享到QQ',
            '分享到微博'
        ]
        wx.showActionSheet({
            itemList: itemList,
            itemColor: '#405f80',
            success: function (res) {
                // res.cancel 用户是不是点击了取消按钮
                // res.tapIndex 数组元素的序号，从0开始
                wx.showModal({
                    title: '用户' + itemList[res.tapIndex],
                    content: '现在无法实现分享功能'
                })
            }
        })
    },

    // 音乐播放
    onMusicTap: function () {
        var currentPostId = this.data.currentPostId
        var postData = postsData.postList[currentPostId]
        var isPlayingMusic = this.data.isPlayingMusic
        if (isPlayingMusic) {
            wx.pauseBackgroundAudio()
            this.setData({
                isPlayingMusic: false
            })
        } else {
            wx.playBackgroundAudio({
                dataUrl: postData.music.url,
                title: postData.music.title,
                coverImgUrl: postData.music.coverImg
            })
            this.setData({
                isPlayingMusic: true
            })
        }
    }
})